package fat
{

import com.fit4as.fit.*;

public class FixtureNameFixture extends ColumnFixture
{
    public var Table : String;

    public function FixtureName() : String {
        var tableParse : Parse = GenerateTableParse(Table);

        var result : String = fixtureName(tableParse).text();
        if (result == "") return "(missing)";
        return result;
    }

    private function GenerateTableParse(table : String) : Parse {
        var rows : Array = table.split("\n");
        return Parse.createParseFromStrings("table", null, GenerateRowParses(rows, 0), null);
    }

    private function GenerateRowParses(rows : Array, rowIndex : int) : Parse {
        if (rowIndex >= rows.length) return null;

        var cells : Array = rows[rowIndex].split(/\]\s*\[/);

        if (cells.length != 0) {
            cells[0] = cells[0].substring(1); // strip beginning '['
            var lastCell : int = cells.length - 1;
            cells[lastCell] = String(cells[lastCell]).replace(/\]$/, "");  // strip ending ']'
        }

        return Parse.createParseFromStrings("tr", null, GenerateCellParses(cells, 0), GenerateRowParses(rows, rowIndex + 1));
    }

    private function GenerateCellParses(cells : Array, cellIndex : int) : Parse {
        if (cellIndex >= cells.length) return null;

        return Parse.createParseFromStrings("td", cells[cellIndex], null, GenerateCellParses(cells, cellIndex + 1));
    }
}
}